package com.tbq.tbqj_ggl.activity

import android.view.View
import android.widget.TextView
import com.tbq.sdk.L
import com.tbq.sdk.TBQ
import com.tbq.tbqj_ggl.R
import com.tbq.tbqj_ggl.model.Prizes
import com.tbq.tbqj_ggl.utils.RandomUtil
import com.tbq.tbqj_ggl.view.ScratchImageView
import pw.hais.http.Http
import java.util.*

/**
 * 刮刮乐
 * Created by hais1992 on 2017/4/5/005.
 */

class ScratchPrizesGameActivity : ScrachGameBaseActivity(if (TBQ.get("KEY_IS_MOBILE") == "1")
    R.layout.mobile_scratch_prizes_game else R.layout.activity_scratch_prizes_game), ScratchImageView.OnScratchListent, View.OnClickListener {
    override fun onClick(p0: View?) {
        if (scratch_win_prize.isScratch || scratch_prize_1.isScratch || scratch_prize_2.isScratch || scratch_prize_3.isScratch || scratch_prize_4.isScratch || scratch_prize_5.isScratch || scratch_prize_6.isScratch || scratch_prize_7.isScratch || scratch_prize_8.isScratch || scratch_prize_9.isScratch) {
            scratch_win_prize.setComplete(true)
            scratch_prize_1.setComplete(true)
            scratch_prize_2.setComplete(true)
            scratch_prize_3.setComplete(true)
            scratch_prize_4.setComplete(true)
            scratch_prize_5.setComplete(true)
            scratch_prize_6.setComplete(true)
            scratch_prize_7.setComplete(true)
            scratch_prize_8.setComplete(true)
            scratch_prize_9.setComplete(true)
        } else {
            L.showShort("要先刮动涂层才可以直接 兑奖 哟！")
        }
    }

    override fun onScratchItem(view: ScratchImageView?) {
        findViewById(R.id.text_start).setBackgroundDrawable(resources.getDrawable(R.drawable.scratch_game_btzlyc))
    }


    private var isWin: Boolean = false
    private val text_message by lazy { findViewById(R.id.text_message) as TextView }
    private val scratch_win_prize by lazy { findViewById(R.id.scratch_win_prize) as ScratchImageView }
    private val scratch_prize_1 by lazy { findViewById(R.id.scratch_prize_1) as ScratchImageView }
    private val scratch_prize_2 by lazy { findViewById(R.id.scratch_prize_2) as ScratchImageView }
    private val scratch_prize_3 by lazy { findViewById(R.id.scratch_prize_3) as ScratchImageView }
    private val scratch_prize_4 by lazy { findViewById(R.id.scratch_prize_4) as ScratchImageView }
    private val scratch_prize_5 by lazy { findViewById(R.id.scratch_prize_5) as ScratchImageView }
    private val scratch_prize_6 by lazy { findViewById(R.id.scratch_prize_6) as ScratchImageView }
    private val scratch_prize_7 by lazy { findViewById(R.id.scratch_prize_7) as ScratchImageView }
    private val scratch_prize_8 by lazy { findViewById(R.id.scratch_prize_8) as ScratchImageView }
    private val scratch_prize_9 by lazy { findViewById(R.id.scratch_prize_9) as ScratchImageView }


    override fun initDataAndView() {
        scratch_win_prize.setOnScratchListent(this)
        scratch_prize_1.setOnScratchListent(this)
        scratch_prize_2.setOnScratchListent(this)
        scratch_prize_3.setOnScratchListent(this)
        scratch_prize_4.setOnScratchListent(this)
        scratch_prize_5.setOnScratchListent(this)
        scratch_prize_6.setOnScratchListent(this)
        scratch_prize_7.setOnScratchListent(this)
        scratch_prize_8.setOnScratchListent(this)
        scratch_prize_9.setOnScratchListent(this)
        text_message.text = "1.刮奖可以选择多种面额,面额越大奖品越丰富。\n" +
                "2.刮开覆盖层，如果刮奖区任意一个\"奖品\"与开奖区相同，即可获得开奖区奖品。\n" +
                "刮奖区其他的\"奖品\"与开奖区不同的\"奖品\"无效。\n" +
                "3.中奖后选择领取奖品方式。\n" +
                "4.\"再来一次\"即放弃本局刮奖,并消耗相应面额金币进行下一轮刮奖。"

    }

    override fun initDataToStartGame(challengePrize: Prizes, isWin: Boolean) {
        super.initDataToStartGame(challengePrize, isWin)
        viewPrizesMap.clear()

        //重置控件
        scratch_win_prize.resteData()
        scratch_prize_1.resteData()
        scratch_prize_2.resteData()
        scratch_prize_3.resteData()
        scratch_prize_4.resteData()
        scratch_prize_5.resteData()
        scratch_prize_6.resteData()
        scratch_prize_7.resteData()
        scratch_prize_8.resteData()
        scratch_prize_9.resteData()
        fullNumber = 0
        winPrizeIsFull = false

        //加载数据
        this.isWin = isWin
        val prizesList = getRandomPrizeList(data_goods_list!!, challengePrize, isWin)
        Http.displayImageAutoSize(scratch_win_prize, SERVER_URL + challengePrize.Icon)
        Http.displayImageAutoSize(scratch_prize_1, SERVER_URL + prizesList[0].Icon)
        Http.displayImageAutoSize(scratch_prize_2, SERVER_URL + prizesList[1].Icon)
        Http.displayImageAutoSize(scratch_prize_3, SERVER_URL + prizesList[2].Icon)
        Http.displayImageAutoSize(scratch_prize_4, SERVER_URL + prizesList[3].Icon)
        Http.displayImageAutoSize(scratch_prize_5, SERVER_URL + prizesList[4].Icon)
        Http.displayImageAutoSize(scratch_prize_6, SERVER_URL + prizesList[5].Icon)
        Http.displayImageAutoSize(scratch_prize_7, SERVER_URL + prizesList[6].Icon)
        Http.displayImageAutoSize(scratch_prize_8, SERVER_URL + prizesList[7].Icon)
        Http.displayImageAutoSize(scratch_prize_9, SERVER_URL + prizesList[8].Icon)

        viewPrizesMap.put(scratch_win_prize, challengePrize)
        viewPrizesMap.put(scratch_prize_1, prizesList[0])
        viewPrizesMap.put(scratch_prize_2, prizesList[1])
        viewPrizesMap.put(scratch_prize_3, prizesList[2])
        viewPrizesMap.put(scratch_prize_4, prizesList[3])
        viewPrizesMap.put(scratch_prize_5, prizesList[4])
        viewPrizesMap.put(scratch_prize_6, prizesList[5])
        viewPrizesMap.put(scratch_prize_7, prizesList[6])
        viewPrizesMap.put(scratch_prize_8, prizesList[7])
        viewPrizesMap.put(scratch_prize_9, prizesList[8])

        findViewById(R.id.text_start).setOnClickListener(this)
    }

    private val viewPrizesMap = HashMap<View, Prizes>()

    private fun getRandomPrizeList(data_goods_list: List<Prizes>, prizes: Prizes, isWin: Boolean): List<Prizes> {
        //临时奖品池
        val data_goods_list_temp: MutableList<Prizes> = arrayListOf()
        data_goods_list_temp.addAll(data_goods_list)

        val list = ArrayList<Prizes>()
        val allGoodsSize = data_goods_list.size
        while (list.size < 9) {   //随机9个奖品
            val p = data_goods_list_temp[RandomUtil.getRandom(0, data_goods_list_temp.size - 1)]
            if (p.PrizesID != prizes.PrizesID) {
                list.add(p)
                if (allGoodsSize > 10) data_goods_list_temp.remove(p)
            }
        }

        if (isWin) {
            val index = RandomUtil.getRandom(0, 8)
            list.removeAt(index)
            list.add(index, prizes)
        }

        return list
    }

    private var fullNumber = 0
    private var winPrizeIsFull: Boolean = false //是否刮开

    override fun onScratchFull(view: ScratchImageView) {
        if (!isPlayGameing) return
        fullNumber += 1
        if (view.id == R.id.scratch_win_prize) winPrizeIsFull = true
        if (fullNumber == 10 && winPrizeIsFull) {
            showGameResultWindows(isWin)
            findViewById(R.id.text_start).setBackgroundDrawable(resources.getDrawable(R.drawable.scratch_game_btzlyc_hs))
        }

    }
}
